using System;
using System.Collections.Generic;

[Serializable]
public class SuccessInARowAchievement : GameplayAchievement
{
	[Serializable]
	public enum WinType
	{
		games = 0,
		tournaments = 1
	}

	[Serializable]
	public class Stage
	{
		public int stageValue;

		public string reward;
	}

	public const AchievementResource.AchievementType achievementType = AchievementResource.AchievementType.SuccessInARowAchievement;

	public WinType winType;

	public List<Stage> stages = new List<Stage>();

	public override bool shouldShowAchievementStageCompleted
	{
		get
		{
			return model.stageReported < achievedStagesCount;
		}
	}

	public override int achievedStagesCount
	{
		get
		{
			for (int i = 0; i < stages.Count; i++)
			{
				if (model.scoreAchieved < stages[i].stageValue)
				{
					return i;
				}
			}
			return stages.Count;
		}
	}

	public override int totalStages
	{
		get
		{
			return stages.Count;
		}
	}

	public override void OnShowAchievementStageCompleted(int achievedStageCount)
	{
		model.stageReported = achievedStageCount;
		ApplyRewards();
		Save();
	}

	public override void UpdateAchievement(GGStoryAchivementsController.UpdateAchievementParams updateParams)
	{
		GGStoryAchivementsController.UpdateAchievementOnMatchResultParams updateAchievementOnMatchResultParams = updateParams as GGStoryAchivementsController.UpdateAchievementOnMatchResultParams;
		if (updateAchievementOnMatchResultParams != null)
		{
			ReportMatchCompleted(updateAchievementOnMatchResultParams);
			if (shouldShowAchievementStageCompleted)
			{
				achivementController.AchievementAccomplishedNotify(this);
			}
		}
	}

	public void ReportMatchCompleted(GGStoryAchivementsController.UpdateAchievementOnMatchResultParams matchCompleteParams)
	{
		if (ContainsMyWinType(matchCompleteParams.winTypes))
		{
			if (matchCompleteParams.isPlayerWinner)
			{
				model.scoreAchieved++;
			}
			else
			{
				model.scoreAchieved = 0;
			}
			Save();
		}
	}

	public bool ContainsMyWinType(List<WinType> winTypes)
	{
		foreach (WinType winType in winTypes)
		{
			if (winType == this.winType)
			{
				return true;
			}
		}
		return false;
	}

	public override void ApplyRewards()
	{
	}

	public override bool HasCompletedStage(int stageIndex)
	{
		if (stageIndex >= stages.Count)
		{
			return false;
		}
		return stageIndex < achievedStagesCount;
	}

	public override string GetStageText(int stageIndex)
	{
		if (stageIndex >= stages.Count)
		{
			return string.Empty;
		}
		return stages[stageIndex].stageValue.ToString();
	}
}
